package com.summer.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.summer.edu.client.VodClient;
import com.summer.edu.entity.Course;
import com.summer.edu.entity.Video;
import com.summer.edu.mapper.VideoMapper;
import com.summer.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-13
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
    @Autowired
    private VodClient vodClient;
    @Override
    public void removeVideoByCourseId(String courseId) {
        QueryWrapper<Video> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        wrapperVideo.select("video_source_id");
      List<Video> videoList=  baseMapper.selectList(wrapperVideo);
      List<String> videoIds = new ArrayList<>();
        for (int i = 0; i <videoList.size() ; i++) {
            Video video = videoList.get(i);
            String sourceId = video.getVideoSourceId();
            if (!StringUtils.isEmpty(sourceId)){
                videoIds.add(sourceId);
            }
        }
        if (videoIds.size()>0){
            vodClient.deleteBatch(videoIds);
        }
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
